//
//  MyDynamicViewController.m
//  MyLittleBrother
//
//  Created by speeda on 17/8/1.
//  Copyright © 2017年 speeda. All rights reserved.
//

#import "MyDynamicViewController.h"
#import "HomeCell.h"
#import "ArticleModel.h"
#import "OtherInfoViewController.h"

@interface MyDynamicViewController () <UITableViewDataSource,UITableViewDelegate,HomeCellDelegate>
@property (weak, nonatomic) IBOutlet UITableView *myDynamicTableView;
@property (nonatomic, strong) NSMutableArray *myDynamicArray;

@property (nonatomic, assign) NSInteger page;

@end

@implementation MyDynamicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initTableView];
}

- (void)initTableView {

    self.title = @"我的动态";
    self.myDynamicTableView.dataSource = self;
    self.myDynamicTableView.delegate = self;
    self.myDynamicTableView.estimatedRowHeight = 100;
    self.myDynamicTableView.rowHeight = UITableViewAutomaticDimension;
    
    __weak typeof(self) weakSelf = self;
    //默认block方法：设置下拉刷新
    self.myDynamicTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf AFNNewData];
    }];
    [self.myDynamicTableView.mj_header beginRefreshing];
    
    //默认block方法：设置上拉加载更多
    self.myDynamicTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf AFNMoreData];
    }];
}



#pragma mark -- 获取贴子
- (void)AFNNewData {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    NSString *userid = [[NSUserDefaults standardUserDefaults] objectForKey:kUserid];
    dict[@"command"] = @"getarticle";
    dict[@"userid"] = userid;
    dict[@"topicid"] = @"";
    dict[@"circleid"] = @"";
    dict[@"page"] = @"1";
    
    [AFHTTPSTool POSTRequestRequestWebsite:Url AndDictionary:dict success:^(NSURLSessionDataTask *dataTask, id responseObject) {
        
        NSDictionary *dic = responseObject;
        NSString *state = [NSString stringWithFormat:@"%@",dic[@"code"]];
        if ([state isEqualToString:@"000000"]) {
            
            [self.myDynamicTableView.mj_header endRefreshing];
            
            [self.myDynamicArray removeAllObjects];
            
            NSLog(@"获取贴子 = %@",responseObject);
            NSArray *array = [ArticleModel mj_objectArrayWithKeyValuesArray:[dic[@"data"] objectForKey:@"rows"]];
            NSString *pageString = [dic[@"data"] objectForKey:@"page"];
            _page = pageString.integerValue;
            _page++;
            
            [self.myDynamicArray addObjectsFromArray:array];
            
            [self.myDynamicTableView reloadData];
            
        } else {
            
            NSString *errorText = [NSString stringWithFormat:@"%@",dic[@"msg"]];
            [SVProgressHUD showInfoWithStatus:errorText];
        }
        
        
    } failure:^(NSURLSessionDataTask *dataTask, NSError *error) {
        
        [self.myDynamicTableView.mj_header endRefreshing];
        [SVProgressHUD showErrorWithStatus:error.localizedDescription];
    }];
}


- (void)AFNMoreData {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    NSString *userid = [[NSUserDefaults standardUserDefaults] objectForKey:kUserid];
    dict[@"command"] = @"getarticle";
    dict[@"userid"] = userid;
    dict[@"topicid"] = @"";
    dict[@"circleid"] = @"";
    dict[@"page"] = @(_page);
    
    [AFHTTPSTool POSTRequestRequestWebsite:Url AndDictionary:dict success:^(NSURLSessionDataTask *dataTask, id responseObject) {
        
        [self.myDynamicTableView.mj_footer endRefreshing];
        
        NSDictionary *dic = responseObject;
        NSString *state = [NSString stringWithFormat:@"%@",dic[@"code"]];
        if ([state isEqualToString:@"000000"]) {
            
            NSLog(@"获取贴子 = %@",responseObject);
            NSArray *array = [ArticleModel mj_objectArrayWithKeyValuesArray:[dic[@"data"] objectForKey:@"rows"]];
            NSString *pageString = [dic[@"data"] objectForKey:@"page"];
            _page = pageString.integerValue;
            _page++;
            
            [self.myDynamicArray addObjectsFromArray:array];
            
            [self.myDynamicTableView reloadData];
            
        } else {
            
            NSString *errorText = [NSString stringWithFormat:@"%@",dic[@"msg"]];
            [SVProgressHUD showInfoWithStatus:errorText];
        }
        
        
    } failure:^(NSURLSessionDataTask *dataTask, NSError *error) {
        
        [self.myDynamicTableView.mj_footer endRefreshing];
        [SVProgressHUD showErrorWithStatus:error.localizedDescription];
    }];
}


#pragma maek -- UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.myDynamicArray.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"cellID";
    HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"HomeCell" owner:nil options:nil];
        cell = [nibs firstObject];
    }
    ArticleModel *articleItem = self.myDynamicArray[indexPath.row];
    cell.articleItem = articleItem;
    cell.delegate = self;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
}


#pragma mark -- HomeCellDelegate
- (void)homeCellDelegateWithButton:(UIButton *)button {
    
    HomeCell *cell = (HomeCell *)button.superview.superview;
    NSIndexPath *indexPath = [self.myDynamicTableView indexPathForCell:cell];
    ArticleModel *articleItem = self.myDynamicArray[indexPath.row];
    OtherInfoViewController *otherVc = [[OtherInfoViewController alloc] init];
    otherVc.b_userid = articleItem.b_userid;
    [self.navigationController pushViewController:otherVc animated:YES];
}


- (NSMutableArray *)myDynamicArray {

    if (!_myDynamicArray) {
        _myDynamicArray = [NSMutableArray array];
    }
    
    return _myDynamicArray;
}

@end
